﻿'*****************************************************************************
' Project : wpListSincMapper            Version: 1.0.0
' Description: Sharepoint List Sincronizer Web Part, mapping based.
' Authors: Roberto Marcos Asensio, Guillermo Martin Royo
' Release Date : 21-6-2009
' Support Blog: http://sharepointworks.blogspot.com
' Author Web Page : http://www.robertomarcos.com
' CodePlex Web site. http://spListSincMapper.codeplex.com
' License : This project is unde GPL license
'*****************************************************************************

Option Explicit On
Option Strict Off

Imports System
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Serialization


Imports System.Xml
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Timers
'Imports System.Web.UI.HtmlControls


Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.VisualBasic

Imports System.ComponentModel
Imports Microsoft.SharePoint.Utilities

Module MiscTools


    Public Function get_filestream_ftp(ByVal ftpRutaFichero As String, ByVal sUser As String, ByVal sPWD As String) As Stream


        Dim fwr As FtpWebRequest = FtpWebRequest.Create(ftpRutaFichero)
        fwr.Credentials = New NetworkCredential(sUser, sPWD)

        fwr.Method = WebRequestMethods.Ftp.DownloadFile

        Return (fwr.GetResponse.GetResponseStream()) ' retornamos el stream


    End Function

    Public Function crea_fichero_Sharepoint(ByVal site As SPWeb, ByVal destURL As String, ByVal txtStr As String, Optional ByVal overwrite As Boolean = True) As Boolean
        Try
            'Dim site As SPWeb = New SPSite(destURL).OpenWeb()
            'Dim fStream As Stream = File1.PostedFile.InputStream
            'dim contents(fStream.Length) As Byte
            Dim chArr(txtStr.Length - 1) As Char
            chArr = txtStr

            'convertimos un char array a byte array
            Dim contents(txtStr.Length - 1) As Byte
            Dim cnt As Long
            For cnt = 0 To txtStr.Length - 1
                contents(cnt) = Microsoft.VisualBasic.AscW(chArr(cnt))
            Next
            EnsureParentFolder(site, destURL)
            site.Files.Add(destURL, contents, overwrite)

        Catch ex As Exception
            Return False
        End Try

        Return True

    End Function




    Public Function EnsureParentFolder(ByVal parentSite As SPWeb, ByVal destinUrl As String) As String

        destinUrl = parentSite.GetFile(destinUrl).Url

        Dim index As Integer = destinUrl.LastIndexOf("/")
        Dim parentFolderUrl As String = String.Empty

        If index > -1 Then
            parentFolderUrl = destinUrl.Substring(0, index)
            Dim parentFolder As SPFolder = parentSite.GetFolder(parentFolderUrl)

            If Not parentFolder.Exists Then
                Dim currentFolder As SPFolder = parentSite.RootFolder
                Dim folder As String

                For Each folder In parentFolderUrl.Split("/"c)
                    currentFolder = currentFolder.SubFolders.Add(folder)
                Next folder
            End If
        End If

        Return parentFolderUrl
    End Function 'EnsureParentFolder

End Module
